package com.seguriapp.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.seguriapp.R;

/**
 * TODO(pjohnson): Insert description
 * 
 * @author Pablo Johnson (pablo.88j@gmail.com)
 */
public class SelectPositionActivity extends
		android.support.v4.app.FragmentActivity implements OnMapClickListener {

	private GoogleMap mMap;
	private Marker marker;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_positionmap);
		setUpMapIfNeeded();
		mMap.setMyLocationEnabled(true);
		mMap.setOnMapClickListener(this);
		mContext = this;

		final LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
		final Criteria criteria = new Criteria();
		final String provider = service.getBestProvider(criteria, false);
		final Location location = service.getLastKnownLocation(provider);
		final LatLng userLocation = new LatLng(location.getLatitude(),
				location.getLongitude());
		mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));

		if (getIntent().getExtras() != null) {
			marker = mMap.addMarker(new MarkerOptions()
					.position(
							new LatLng(getIntent().getExtras().getDouble(
									"latitud"), getIntent().getExtras()
									.getDouble("longitud")))
					.title("Crimen")
					.snippet("Lugar del crimen")
					.icon(BitmapDescriptorFactory
							.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
		}

		final Button buttonSetear = (Button) findViewById(R.id.setearPosicionButton);
		buttonSetear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (marker != null) {
					final Intent intent = new Intent();
					final Bundle data = new Bundle();
					data.putDouble("latitud", marker.getPosition().latitude);
					data.putDouble("longitud", marker.getPosition().longitude);
					intent.putExtras(data);
					setResult(Activity.RESULT_OK, intent);
					finish();
				} else {
					Toast.makeText(mContext,
							mContext.getString(R.string.locate_warning),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		setUpMapIfNeeded();
	}

	private void setUpMapIfNeeded() {
		if (mMap == null) {
			mMap = ((SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.map)).getMap();
		}
	}

	@Override
	public void onMapClick(LatLng position) {
		mMap.clear();
		marker = mMap.addMarker(new MarkerOptions()
				.position(position)
				.title("Crimen")
				.snippet("Lugar del crimen")
				.icon(BitmapDescriptorFactory
						.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
	}

}
